//判断token
if (localStorage.getItem("token") == null) {
    location.href = '/bigEvent127/login.html';
}
// -----请求数据
$.ajax({
    url: 'http://ajax.frontend.itheima.net/my/userinfo',
    // 请求头
    headers:{
        'Authorization': localStorage.getItem("token")
    },
    success: function (res) {
        if (res.status == 0) {
            let obj = res.data;
            console.log(obj)
            // obj.nickname = 'cc'
            if (obj.user_pic != null) {
                $(".userinfo img").show().prop('src',obj.user_pic)
            }
           else if (obj.nickname != "") {
                let t = obj.nickname.substr(0,1).toUpperCase()
                $('.userinfo .avatar').show().css('display', 'inline-block').text(t)
            }
           else if (obj.username != "") {
                let t = obj.username.substr(0, 1).toUpperCase()
                $('.userinfo .avatar').show().css('display', 'inline-block').text(t)
                
            }
        }
    },
    complete: function (res) {
       
            // 原生对象；
            let obj = JSON.parse(res.responseText);
            if (obj.status == 1 && obj.message == "身份认证失败！") {
        
              // 回到登录
              location.href = "/login.html";
        
              // 清除token
              localStorage.removeItem("token");
        
            }
          
    }

})

// ---------退出
let layer = layui.layer
$('#logout').on('click', function () {
    layer.confirm("您是否真的要退出", function (index) {
        layer.close(index)
        //回到登录
        location.href = "/bigEvent127/login.html"
        //清楚token
        localStorage.removeItem("token")
        layer.close(index)
    })
})

// $('#logout').click(function () {
//     // 弹出层，询问是否要退出
//     layer.confirm('你确定退出吗？你退出了还得登录，你想好了吗？', function (index) {

//         // 如果点击了确定，删除token，页面跳转
//         localStorage.removeItem('token');
//         location.href = '/bigEvent127/login.html';
//         layer.close(index); // 关闭当前弹出层
//     });
// });